Skip to content

agx: never spill to scratch in the remat-only pass, drop the preamble retry - #1102

Merged
DjDeveloperr merged 1 commit into
aurora-wipfrom
agx/remat-no-scratch-v2
Nov 25, 2026
Merged

DjDeveloperr merged 1 commit into
aurora-wipfrom
agx/remat-no-scratch-v2

Conversation

@RyanTheTide

Copy link
Copy Markdown

What

The remat-only spill pass never moves a value it cannot rematerialize out of the register file. agx_ra() measures the result and finishes under the full register file when the estimate is missed, on every chip. The AGX3C preamble NIR-clone retry, agx_ra_without_remat() and the ASAHI_M5_CPROG_REMAT_RETRY knob go. Replaces #547, rebuilt as one signed commit on current aurora-wip (b257b3b0148).

Tested: x86 agx_tests 1617 passed, 1 skipped, 0 failed (aurora-mesa-xbuild container, debugoptimized). Hardware rows from before the rebase are in the details. A fresh isolated AGX2 row at this head is still owed.
Risk: medium. Generic spiller and register-allocation change. Since #603 it only changes code generation on AGX2 (G13, G14) and AGX3D (G18G), where the wave-saving remat runs, and only for shaders whose remat-only pass used to spill to scratch.

Not yet

  • Isolated AGX2 A/B on this head (M1 Pro G13S or M2 G14G): conformance + gpubench-vk, with 0 AVC, 0 failed units and 0 new kernel errors, ideally on a shader that reaches the remat-only path. Requested as a batched lab row.
  • AGX3D (M6 G18G) hardware row once an M6 owner can run this head.
Review items from #547 (Dj, at ee7ec1a)
  1. Conflicts with aurora-wip: rebuilt on current aurora-wip. Kept !ctx->ra_mem_exhausted in the ctx->ra_failed retry, kept agx_ra_find_regs_simple(), agx_ra_mem_bound() and agx_licm_demand_bound(), and changed the agx_compile_function_nir_attempt() comment in agx_ra_mem_bound() to agx_compile_function_nir(). The unified full-budget fallback in agx_ra() keeps the newer no-scratch refusal (!has_scratch sets ra_failed) and the ra_failed exits after each agx_spill(). It replaces the G18G-only branch and the other-chip branch.
  2. DCO: one commit with a human author and Signed-off-by, no merge commits, no AI trailers. agx: never spill to scratch in the remat-only pass, drop the preamble retry #547 is closed instead of force-pushed.
  3. CI: runs on this PR's head.
  4. Hardware: still owed (see Not yet).

Also changed: aurora-wip's newer SpillFallback.NoScratchSpillFailsClosed expected the remat-only pass to spill phis to memory on a no-scratch shader and then refuse. With this change that spill cannot happen, so case 1 now checks the stronger property: the pass emits no memory value at all and does not fail. Case 2 (direct over-budget allocation on a no-scratch helper) runs agx_ra() and still fails closed with 0 memory ops.

Earlier description and rows from #547

Problem

For AGX3C fragment preambles the compiler compiled the preamble on a NIR clone and, if the remat-only spill pass had spilled to scratch, threw the result away and compiled again without remat (ASAHI_M5_CPROG_REMAT_RETRY, on by default). The retry hid the real issue: when recreating constants at their uses was not enough to reach the estimated register budget, the remat-only pass moved values it cannot recreate out to scratch.

Change

The remat-only pass never moves a value it cannot recreate out of registers. It keeps those values even past the estimate, and phis whose missing sources are constants stay in registers. Every recreated constant sits inside the live range of the value it replaces, so the result never needs more registers than the original program; agx_ra() measures the result and uses the full register file when the estimate was missed (this was already the G18G path; every chip now shares it). Removed: the NIR-clone retry wrapper, agx_ra_without_remat(), the disable_remat / retry_without_remat parameters and the ASAHI_M5_CPROG_REMAT_RETRY knob.

Output only changes for shaders where the remat-only pass used to emit a scratch store; those lose that scratch traffic.

Since #603 (merged into this branch at d220af7)

#603 (Dynamic Caching) runs the wave-saving rematerialization only where the register file is split statically (agx_has_static_register_split(): AGX2 and AGX3D). On the merged tree:

  • AGX3B and AGX3C (G17P, G17S, M5) never reach the remat-only pass. This PR removes only dead code there: the AGX3C preamble retry could no longer fire. The ssbo.phys.layout.random.scalar.13 change described below happened before the merge and cannot happen any more.
  • The remat-only pass still runs on AGX2 (G13, G14) and AGX3D (G18G). This is where the change matters: values that cannot be rematerialized stay in registers, and the pass never spills them to scratch.
  • The merge keeps the single agx_ra() with agx: AGX3 Dynamic Caching: last uses in divergent control flow, no static-split register budget #603's static-split condition on the remat. ee7ec1ae946 runs both remat-only unit tests on G14G (AGX2) and G18G (AGX3D) keys as well. With keep_in_registers() disabled, every case fails on all three keys.

Shim corpus on the merged head (ee7ec1ae946 tree, AGX_MESA_DEBUG=shaderdb) against aurora-wip ee3abd081e4: 0 changed shaders on every chip, with identical spill and scratch totals.

chip cases shader stat lines changed
G13X, G14G (AGX2) 26,602 each 28,271 / 28,270 0
G18G (AGX3D) 3,108 (glsl + graphicsfuzz part) 5,404 0
G17P (AGX3B) 26,602 48,509 0
G17S (AGX3C) 14,377 15,992 0

The corpus is all of dEQP-VK.ssbo.* plus every second case of glsl loops/operator/functions/conditionals/switch/indexing, graphicsfuzz and compute.pipeline. On G18G only the glsl + graphicsfuzz part runs on the shim. The ssbo/compute cases hit an existing nir_lower_explicit_io assert on both arms (the SSBO address format that #561 fixes), and cooperative_matrix cases stall the G18G shim. x86 clang + drm-shim asahi + poly suites on the merged head: 223/223.

Testing (before the #603 merge)

Affects every chip, but output only changes for shaders whose remat-only pass used to spill to scratch. Rows:

Hardware rows used one combined build of all sweep branches (sweep/all5 = aurora-wip c55152d + this and the sibling sweep branches) against aurora-wip c55152d, both gcc release builds with identical meson options, loaded as user-space overlays (VK_DRIVER_FILES / LD_LIBRARY_PATH) on the production kernels; nothing installed, no reboot.

machine (GPU) kernel dEQP-VK subset, 14,869 cases GLES/GL CTS subset, 5,079 cases gpubench-vk (2 interleaved runs per arm)
A18 Pro MacBook Neo (G17P) 7.3.0-40 11262 Pass / 3607 NotSupported / 0 Fail, 0 changed 4975 Pass, 0 changed all rows within run-to-run spread (single-submit rows rechecked with 4 runs)
M4 Pro (G17P) 7.3.0-aurora-fleet75 11262 Pass / 3607 NotSupported / 0 Fail, 0 changed 4975 Pass, 0 changed all rows flat except the queue timeline hop (16.1/17.5 vs 19.2/20.0 us); that row is bimodal, no branch touches its path on the M4 Pro, and it is flat on the A18 Pro; a 5-run recheck is pending
M2 (G14G) 7.3.0-aurora-fleet74 7946 Pass / 6523 NotSupported / 0 Fail, 0 changed (sweep/all4) n/a all rows within +-2 % except render.light d1, bimodal (194-222 us) in both arms
M1 Pro (G13S) 7.3.0-aurora-fleet74 16,864-case variant of the subset (adds pipeline sample_mask/timestamp and image load/store): 9571 Pass / 7293 NotSupported / 0 Fail, 0 changed 4023 Pass, 0 changed flat except compute.heavy alu d1: 5-run recheck 1101-1163 vs 1121-1196 us (median +4.7 %); d4/d16 identical, the same row is identical on the M2 (same AGX2 compiler path) and flat in a clang-built pair on this Mac, so most likely run-state noise; a per-branch isolation is still to run

Real shaders that reach the changed path. Compiled on the drm-shim with AGX_MESA_DEBUG=shaderdb, base aurora-wip 1105ea3a6e5 vs this branch (a723d14606e). In 12,162 dEQP-VK.ssbo.* cases the remat-only pass runs 568 times on G17S. Exactly one shader in that set, ssbo.phys.layout.random.scalar.13, used to spill, and it is the only shader whose output changes on any chip:

chip base this branch
G17S (AGX3C) 11088 instrs, 2:2 spills:fills, 8 B scratch 11077 instrs, 0:0, no scratch
G17P (AGX3B) 1352 instrs, 2:2 spills:fills, 8 B scratch 1343 instrs, 0:0, no scratch
G14G, G13X no change no change

Register count (111) and threads (896) are the same in both builds, so occupancy is unchanged.

On the M4 Pro (J616s, G17P, 7.3.0-aurora-fleet78), the same case compiled on the device gives the same stats, 2:2 / 8 B before and 0:0 / none after, and passes 3/3 on both builds. All 100 cases of ssbo.phys.layout.random.scalar.* pass on both.

A fragment-heavy corpus (87,014 cases: glsl, ubo, graphicsfuzz, spirv_assembly graphics, texture) on G17S ran the remat-only pass only twice, with no scratch spill and no preamble retry. So the removed AGX3C preamble retry never fired on these shaders. On 11,942 of those cases (13,193 fragment shaders, 11,201 AGX3 preambles) neither build changes anything or spills.

Still to run: G18G (M6) needs a hardware row, because AGX3D is where the remat-only pass still runs, and we have no M6 in our lab. The AGX2 rows above (M1 Pro, M2) were taken before the merge. #603 does not change the AGX2 path, and the shim corpus shows no change there on the merged head. A merged-head M1 Pro + M2 no-change row is queued. The M5 (G17G) row requested earlier is no longer needed: on the merged tree this PR cannot change M5 output.

Method
  • dEQP-VK subset (Vulkan CTS 1.4.7.0): compute.pipeline., synchronization(2).op.single_queue.{barrier,event,timeline_semaphore}., synchronization.timeline_semaphore., synchronization.global_priority_transition., api.device_init.create_device_global_priority*, memory_model.{message_passing,shared}., ssbo(.phys).layout.random., glsl.{opaque_type_indexing,operator,loops}., spirv_assembly.instruction.{graphics.16bit_storage,compute.float_controls}., api.copy_and_blit buffer/image copies, clipping.user_defined., query_pool.statistics_query.fragment_shader_invocations., tessellation.{primitive_discard,winding,shader_input_output}.* (large groups subsampled 1/2 to 1/8).
  • GLES/GL: every second case of a 13k dEQP-GLES2/3/31 + KHR-GL46 list (shaders, texture, fbo, ssbo, copy_image, fragment_ops, ubo, packed_pixels).
  • gpubench-vk 0dae261: submit.empty, compute.light, render.light, copy.bw {buf2buf, buf2img, fill}, sync.signal {queue timeline, host}, compute.heavy {alu, bw}, render.heavy fill 1080p/2160p, query timestamp; --warmup-ms 300 --runs 5 --run-ms 250, arms interleaved, 70 C cool-down gate.
  • Kernel log, GPU fault count, failed units and AVCs checked before and after each run: no new entries.
Offline check

3031 offline compiles (libagx for every precompiled target plus every unit-test compile) produced bit-identical binaries, register counts and scratch sizes against aurora-wip. Only 3 of them ran the remat pass, so this check is thin on that path; the hardware rows above cover it.
New/changed unit tests in test-spill-fallback.cpp: RematOnlyKeepsPhiWebsInRegisters (no scratch at all) and RematOnlyRematerializesConstantsWithoutScratch (G17G fragment preamble, 32 halves against a 24-half budget).

Corpus method
  • x86 deqp-vk (VK-GL-CTS 1.4.7.1, surfaceless) against x86 clang builds of both trees with asserts on, through the asahi drm-shim (ASAHI_SHIM_CHIP=g17s-kernel|g17p|g14g|g13x; submits are no-ops, compiles are real).
  • Per-case AGX_MESA_DEBUG=shaderdb lines compared between builds. Remat-only pass and spill counts came from a separate base build with a stderr print in agx_ra(), which is not part of this PR.
  • Review follow-up in a723d14606e: the three remat-only invariant asserts now also bound nW by AGX_NUM_REGS, so W_entry and W_exit cannot overrun. The remat_fallback_spilled and insert_spill() comments describe the new behaviour. x86 asahi suite: everything passes except hk_dual_source_gpl, which fails the same way on the base (a clang-only failure fixed on aurora-wip by asahi: keep the merged PPP scratch words 4-byte aligned #562).

Builds on aa2d33a.

… retry

The occupancy rematerialization pass (agx_spill() with remat_only) only
runs when the program already fits the full register file. It lowers
pressure towards a smaller, estimated budget by recreating constants at
their uses. When rematerialization alone could not reach that estimate,
it moved values it cannot rematerialize to scratch. Register allocation
then finished under the full budget but kept those scratch spills, and
G17G-family (AGX3C) fragment preambles were recompiled from a NIR clone
without rematerialization to get rid of them (ASAHI_M5_CPROG_REMAT_RETRY,
on by default).

Make the remat-only pass pressure-aware instead: it never moves a value
it cannot rematerialize out of the register file. limit() and the W
selection at block entries and loop headers keep such values even when
that exceeds the estimate, and a phi whose missing sources are constants
stays in a register (the constants are rematerialized along the edge).
Every rematerialized value is recreated inside the live range of the
value it replaces, so the result never needs more registers than the
original program. agx_ra() measures it and allocates under the full
file when the estimate was missed, which it did only for G18G; every
chip now takes that one path, keeping the no-scratch refusal and the
ra_failed exits of the full-budget spill.

The remat-only pass may exceed its estimate, so the nW <= k asserts in
min_algorithm() and the two W-entry computations are relaxed for it, but
an explicit nW <= AGX_NUM_REGS bound stays so a broken assumption fails
the assert instead of overrunning W_entry/W_exit. remat_fallback_spilled
is now a backstop for memory values that other paths create.

This is generic spiller logic with no hardware dependence. Programs whose
remat-only pass met its budget are unchanged. The preamble recompile,
its NIR clone, agx_ra_without_remat() and the ASAHI_M5_CPROG_REMAT_RETRY
knob are removed.

Tests: both remat-only spill tests also run on a G14G (AGX2) and a G18G
(AGX3D) key, where production reaches this pass; with
keep_in_registers() disabled every case fails on all keys. In
NoScratchSpillFailsClosed the remat-only case now checks that the pass
emits no memory value at all on a no-scratch shader (it used to expect
the spill and the refusal), and the direct over-budget case runs agx_ra().
x86 agx_tests: 1617 passed, 1 skipped, 0 failed.

Builds on aa2d33a by DjDeveloperr.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Audited full diff across agx_compile.c, agx_compiler.h, agx_register_allocate.c, agx_spill.c, and test-spill-fallback.cpp: clean architectural simplification that removes the AGX3C-only preamble NIR-clone retry (ASAHI_M5_CPROG_REMAT_RETRY) and agx_ra_without_remat(), keeps non-rematerializable values in registers during the remat-only pass via keep_in_registers() across limit()/compute_w_entry_loop_header()/compute_w_entry(), and uniformly measures post-remat demand in agx_ra() across all encodings without a G18G-only branch. Covered by updated SpillFallback unit tests across AGX2/AGX3B/AGX3C/AGX3D.

@RyanTheTide

Copy link
Copy Markdown
Author

75696cd205a6 dual-board assert-enabled (-Db_ndebug=false, MESA_SHADER_CACHE_DISABLE=1, zero reboot) Borealis Vulkan overlays PASS:

  • J316s (Apple M1 Pro (G13S C0), AGX2, 7.3.0-aurora-fleet89, paired A/B vs exact aurora-wip b257b3b0148 9020fad26512084a, overlay 9c8432bff57cd55d): exact A/B parity (992 Pass / 0 Fail / 12 NS vs baseline 992/0/12) across dEQP-VK.glsl.loops.* 624/624, dEQP-VK.ssbo.layout.single_basic_array.* 351/351, dEQP-VK.draw.renderpass.linear_interpolation.* 9/21 (12 NS), dEQP-VK.api.smoke.* 8/8; 0 assertions, 0 GPU faults, 0 new kernel errors, 0 new AVC, 0 failed units, Enforcing; stock Borealis verified.
  • J616s (Apple M4 Pro (G16S B1), AGX3B, 7.3.0-aurora-fleet89, batched cleanly with #1097 on fd5484a244f4, 0 merge-tree diffs, overlay 778fb46e7fb036e9): dEQP-VK.glsl.loops.* 624/624, dEQP-VK.ssbo.layout.single_basic_array.* 351/351, dEQP-VK.glsl.matrix.inverse.* 12/12, dEQP-VK.api.smoke.* 8/8 (995/995 Pass / 0 Fail); 0 assertions, 0 GPU faults, 0 new kernel errors, 0 new AVC, 0 failed units, Enforcing; stock Borealis verified.

@DjDeveloperr
DjDeveloperr merged commit 1cccd44 into aurora-wip Nov 25, 2026
@RyanTheTide
RyanTheTide deleted the agx/remat-no-scratch-v2 branch November 27, 2026 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants